<?php

namespace Home\Controller;

use Think\Controller;

/**
 * 登录认证
 */
class LoginController extends Controller {

    public function index() {
        $this->display('index');
    }

    /**
     * 登录
     */
    public function login() {
        //非正常访问
        if (empty($_POST)) {
            $this->error('非法访问！');
        }
        cookie('name', I('post.username'));
        cookie('pwd', I('post.pwd'));
//        $very = new \Think\Verify();
//        if (!$very->check(I('post.verifycode'))) {//需要验证码
//            //if($very->check(filter_input(INPUT_POST,'code'))){//不需要验证码
//            $this->success('验证码错误');
//            exit;
//        }
        $data['userAccount'] = I('post.username');
        $data['password'] = encrypt(I('post.pwd'));
        if (empty($data['userAccount'])) {
            $this->error('您的输入错误，请认真填写',U(c('login')));
        }
        $Model = M('user');
        $result = $Model->field('useraccount,name,identity')->where($data)->find();
        //账号不存在
        //p($result);
        if(empty($result)){
            $this->error('账号或密码错误,请认真填写.',U(c('login')));
        }
        //p(array('admintimestamp'=> getTime()));
        $sql = "UPDATE  tp_user SET adminTimeStamp='".getTime()."' where userAccount='".$result['useraccount']."'";
        M()->execute($sql);
        session('account',$result['useraccount']);
        session('name',$result['name']);
        session('identity',$result['identity']);
        if($result['identity']!=1){
             $this->redirect('Home/Home/index');
        }else{
            $this->redirect('Admin/Index/index');
        }
    }

    /**
     * 退出 
     */
    public function dropOut() {
        session(null);
        $this->redirect('Home/Index/index');
    }

    /**
     * 找回密码
     */
    public function retrieve() {
        $arr = filter_input_array(INPUT_POST);
        if (empty($arr['hao'])) {
            $this->ajaxReturn(2);
            exit();
        }
        $t = new \Tool\Code($arr['hao'], $arr['email'], $arr['i']);
        $i = $t->flow();
        $this->ajaxReturn($i);
    }

    /**
     * 教师找回密码
     */
    public function alterTeaWd() {
        $arr = filter_input_array(INPUT_POST);
        empty($arr['hao']) ? exit : 1;
        if (!(session('email') == $arr['code'])) {
            $this->error('验证码错误');
        }
        $data['pwd'] = encrypt($arr['pwd1']);
        $arr['tea_number'] = $arr['hao'];
        $Model = M('Teacher');
        $t = $Model->create($arr);
        if ($Model->where($t)->save($data)) {
            $this->success('修改成功');
        } else {
            $this->error('未修改');
        }
    }

    /**
     * 学生修改密码，
     */
    public function alterStuWd() {
        $arr = filter_input_array(INPUT_POST);
        empty($arr['hao']) ? exit : 1;
        if (!(session('email') == $arr['code'])) {
            $this->error('验证码错误');
        }
        $data['pwd'] = encrypt($arr['pwd1']);
        $arr['stu_number'] = $arr['hao'];
        $Model = M('Student');
        $t = $Model->create($arr);

        if ($Model->where($t)->save($data)) {
            $this->success('修改成功');
        } else {
            $this->error('未修改');
        }
    }

}
